package org.blueoxygen.rest.geneology.action;

import org.apache.struts2.convention.annotation.Action;
import org.blueoxygen.rest.geneology.Person;

@Action("parent")
public class PersonsParentController extends PersonController {

	public String show() {
		person = (Person) session.get(Person.class, id);

		return "";
	}

	public String f() {
		person = (Person) session.get(Person.class, id);
		String hql = "FROM " + Person.class.getName()
				+ " WHERE male = true AND id != '" + id + "'";
		persons = session.createQuery(hql).list();

		return "person/person-f-list";
	}

	public String m() {
		person = (Person) session.get(Person.class, id);
		String hql = "FROM " + Person.class.getName()
				+ " WHERE male = false AND id != '" + id + "'";
		persons = session.createQuery(hql).list();

		return "person/person-m-list";
	}

	public String c() {
		person = (Person) session.get(Person.class, id);
		String parent = person.isMale() ? "father" : "mother";
		String hql = "FROM " + Person.class.getName() + " WHERE " + parent
				+ " IS NULL";
		persons = session.createQuery(hql).list();

		Person temp = person;

		while(true) {
			if(temp.getFather() != null) {
				temp = temp.getFather();
			} else {
				persons.remove(temp);
				break;
			}
		}

		while(true) {
			if(temp.getMother() != null) {
				temp = temp.getMother();
			} else {
				persons.remove(temp);
				break;
			}
		}

		return "person/person-c-list";
	}

	public String addF() {
		person = (Person) session.get(Person.class, id);
		Person parent = (Person) session.get(Person.class, request
				.getParameter("pid"));
		person.setFather(parent);

		session.beginTransaction();
		session.saveOrUpdate(person);
		session.getTransaction().commit();

		return "person/person-parent-success";
	}

	public String addM() {
		person = (Person) session.get(Person.class, id);
		Person parent = (Person) session.get(Person.class, request
				.getParameter("pid"));
		person.setMother(parent);

		session.beginTransaction();
		session.saveOrUpdate(person);
		session.getTransaction().commit();

		return "person/person-parent-success";
	}

	public String addC() {
		person = (Person) session.get(Person.class, id);
		Person child = (Person) session.get(Person.class, request
				.getParameter("cid"));
		if (person.isMale()) {
			child.setFather(person);
		} else {
			child.setMother(person);
		}

		session.beginTransaction();
		session.saveOrUpdate(child);
		session.getTransaction().commit();

		return "person/person-parent-success";
	}

	public String rmC() {
		person = (Person) session.get(Person.class, id);
		Person child = (Person) session.get(Person.class, request
				.getParameter("cid"));
		if (person.isMale()) {
			child.setFather(null);
		} else {
			child.setMother(null);
		}

		session.beginTransaction();
		session.saveOrUpdate(child);
		session.getTransaction().commit();

		return "person/person-parent-success";
	}

	public String getId() {
		return id;
	}

}
